#include "searchpanel.h"
#include <QHBoxLayout>
#include "codeeditor.h"
#include "resources.h"

SearchPanel::SearchPanel(QWidget *parent) :
    QWidget(parent)
{
    QHBoxLayout * layout = new QHBoxLayout;
    setLayout(layout);

    m_searchForLabel = new QLabel("Search For:");
    m_replaceWithLabel = new QLabel("Replace With:");
    QPixmap down;
    down.loadFromData(Resources::down_png, sizeof(Resources::down_png));
    m_forwardButton = new QPushButton(QIcon(down), "");
    QPixmap up;
    up.loadFromData(Resources::down_png, sizeof(Resources::up_png));
    m_backwardButton = new QPushButton(QIcon(up), "");
    m_replaceAllButton = new QPushButton("Replace All");
    //m_replaceInSelectionButton = new QPushButton("Replace In Selection");
    QPixmap done;
    done.loadFromData(Resources::done_png, sizeof(Resources::done_png));
    m_doneButton = new QPushButton(QIcon(done), "Done");
    m_replaceEdit = new QLineEdit;
    m_findEdit = new QLineEdit;
    layout->addWidget(m_searchForLabel);
    layout->addWidget(m_findEdit);
    layout->addWidget(m_backwardButton);
    layout->addWidget(m_forwardButton);
    layout->addWidget(m_replaceWithLabel);
    layout->addWidget(m_replaceEdit);
    layout->addWidget(m_replaceAllButton);
    //layout->addWidget(m_replaceInSelectionButton);
    layout->addWidget(m_doneButton);

    connect(m_findEdit, SIGNAL(textChanged(QString)), this, SLOT(onFindTextChanged(QString)));
    connect(m_forwardButton, SIGNAL(pressed()), this, SLOT(onForwardPressed()));
    connect(m_backwardButton, SIGNAL(pressed()), this, SLOT(onBackwardPressed()));
    connect(m_replaceAllButton, SIGNAL(pressed()), this, SLOT(onReplaceAllPressed()));
    //connect(m_replaceInSelectionButton, SIGNAL(pressed()), this, SLOT(onReplaceInSelectionPressed()));
    connect(m_doneButton, SIGNAL(pressed()), this, SLOT(onDonePressed()));


}

SearchPanel::~SearchPanel()
{
    delete m_searchForLabel;
    delete m_replaceWithLabel;
    delete m_forwardButton;
    delete m_backwardButton;
    delete m_replaceAllButton;
    //delete m_replaceInSelectionButton;
    delete m_doneButton;
    delete m_replaceEdit;
    delete m_findEdit;
}

void SearchPanel::setEditor(CodeEditor *aEditor)
{
    m_editor = aEditor;
}

void SearchPanel::setFindFocus()
{
    m_findEdit->setFocus();
}




void SearchPanel::onFindTextChanged(QString aNewText)
{
    if(aNewText.length() == 0)
        return;
    if(!m_editor)
        return;
    m_editor->findAndScrollToWord(aNewText);
}

void SearchPanel::onForwardPressed()
{
    if(m_findEdit->text().length() == 0)
        return;
    if(!m_editor)
        return;
    m_editor->findNextMatch(m_findEdit->text());
}


void SearchPanel::onDonePressed()
{
    hide();
}

/*
void SearchPanel::onReplaceInSelectionPressed()
{
    if(m_findEdit->text().length() == 0)
        return;
    if(!m_editor)
        return;
    m_editor->replaceInSelection(m_findEdit->text(), m_replaceEdit->text());
}
*/

void SearchPanel::onReplaceAllPressed()
{
    if(m_findEdit->text().length() == 0)
        return;
    if(!m_editor)
        return;
    m_editor->replaceAll(m_findEdit->text(), m_replaceEdit->text());
}


void SearchPanel::onBackwardPressed()
{
    if(m_findEdit->text().length() == 0)
        return;
    if(!m_editor)
        return;
    m_editor->findPreviousMatch(m_findEdit->text());
}


void SearchPanel::keyPressEvent(QKeyEvent *e)
{
    int key = e->key();
    if(key == Qt::Key_Escape)
    {
        hide();
    }
    QWidget::keyPressEvent(e);
}


void SearchPanel::hideEvent ( QHideEvent * event )
{
    emit onHide();
    QWidget::hideEvent(event);
}
